//
//  HomeInfoTopheaderView.m
//  GuShiNews
//
//  Created by huachun on 2019/8/22.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "HomeInfoTopheaderView.h"
#import "LunTanModel.h"

@interface HomeInfoTopheaderView ()

@property (nonatomic, strong) UILabel *Label_tittle;
@property (nonatomic, strong) UILabel *Label_type;

@end

@implementation HomeInfoTopheaderView

/* 在构造方法中，创建UI*/
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.Label_tittle];
        [self addSubview:self.Label_type];
        
        NSArray *linlist = @[@" 点赞",@" 跟帖"];
        CGFloat w = RECRWITCH/linlist.count;
        [linlist enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(w*idx, 110, w, 40)];
            button.tag = idx;
            [button setTitle:obj forState:UIControlStateNormal];
            [button addTarget:self action:@selector(chooseindex:) forControlEvents:UIControlEventTouchUpInside];
            [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [self addSubview:button];
            
            CALayer *line = [CALayer new];
            line.frame=CGRectMake(0, 0, w, 2);
            line.backgroundColor = [UIColor whiteColor].CGColor;
            [button.layer addSublayer:line];
            
            CALayer *linehen = [CALayer new];
            linehen.backgroundColor = [UIColor whiteColor].CGColor;
            [button.layer addSublayer:linehen];
            
            if (idx==0) {
                linehen.frame=CGRectMake(w-1, 0, 1, 40);
                [button setTitleColor:[JonesClass stringTOColor:@"#d81e06"] forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:@"lunzan"] forState:UIControlStateNormal];
                self->_zan_button = button;
            }else{
                linehen.frame=CGRectMake(0, 0, 1, 40);
                [button setTitleColor:[JonesClass stringTOColor:@"#069d17"] forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:@"lunfabu"] forState:UIControlStateNormal];
                self->_hui_button = button;
            }
        }];
    }
    return self;
}

- (void)chooseindex:(UIButton *)sender{
    _selectblock (sender.tag);
}


- (void)setModel:(LunTanModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    
    NSDictionary *dic = @{NSKernAttributeName:@0.f};
    NSMutableAttributedString * attributedString =     [[NSMutableAttributedString alloc] initWithString:_Model.tittle attributes:dic];
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]     init];
    [paragraphStyle setLineSpacing:4];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_Model.tittle length])];
    [_Label_tittle setAttributedText:attributedString];
    [_Label_tittle sizeToFit];
    
    
    _Label_tittle.frame=CGRectMake(15, 10, RECRWITCH-30, _Model.Height);
    _Label_type.frame = CGRectMake(15, _Model.Height + 20, RECRWITCH-30, 20);
    _Label_type.text=[NSString stringWithFormat:@"#%@#",_Model.type];
    
    _zan_button.frame = CGRectMake(0, _Model.Height + 50, RECRWITCH/2, 40);
    _hui_button.frame = CGRectMake(RECRWITCH/2, _Model.Height + 50, RECRWITCH/2, 40);
    
    if ([_Model.zan_count intValue]>0) {
        [_zan_button setTitle:[NSString stringWithFormat:@" 点赞(%@)",_Model.zan_count] forState:UIControlStateNormal];
    }
    if ([_Model.ping_count intValue]>0) {
        [_hui_button setTitle:[NSString stringWithFormat:@" 跟帖(%@)",_Model.ping_count] forState:UIControlStateNormal];
    }
}

-(UILabel *)Label_tittle{
    if (!_Label_tittle) {
        _Label_tittle           = [[UILabel alloc] init];
        _Label_tittle.font      = [UIFont boldSystemFontOfSize:20];
        _Label_tittle.textColor = NewsTitleColor;
        _Label_tittle.numberOfLines = 0;
        _Label_tittle.frame=CGRectMake(15, 10, RECRWITCH-30, 50);
    }
    return _Label_tittle;
}

-(UILabel *)Label_type{
    if (!_Label_type) {
        _Label_type           = [[UILabel alloc] init];
        _Label_type.font      = [UIFont systemFontOfSize:14];
        _Label_type.textColor = NavItemTitleColor;
        _Label_type.frame = CGRectMake(15, 70, RECRWITCH-30, 20);
        _Label_type.text=@"#海尔股份#";
    }
    return _Label_type;
}

@end
